thin.imports('/public/home/css/global.project.css');

thin.imports('/public/configure/conf.project.js');
thin.imports('/public/home/resource/pro-delete.js');

thin.use(function() {
	checkLocation();
	$('#project-lists').on('click', '#btn-keyPush', isPerfectProject).on('click', '#btn-cph', function() {
		var _id = $(this).attr('data-id');
		if(!chpdata[_id]) {
			$.showLoading();
			__getdata_procph['project_id'] = $(this).attr('data-id');
			factory.request('_project_cphistory', _id);
		} else {
			createCph(chpdata[_id]);
		}
	}).on('click', '.btn-jncxbzj', function() {
		var _proid = $(this).attr('data-id');
		// 缴纳诚信保证金
		var param = {
			mark: 'cx',
			proid: _proid,
			reurl: '/mobile/chain/fund'
		};
		window.location.href = '/mobile/pay/index.html?param=' + encodeURIComponent(JSON.stringify(param));
	}).on('change', '#weuiAgree', function() {
		// 监听协议按钮
		var ischecked = $(this).prop('checked'),
			_p = $(this).parent().parent();
		if(ischecked) {
			_p.find('#btn-keyPush').removeClass('off').addClass('on');
		} else {
			_p.find('#btn-keyPush').removeClass('on').addClass('off');
		}
	});
	
	$('#cph-modal-list').on('click', '#btn-delpdf', function() {
		var _id = $(this).attr('data-id');
		$.confirm("确定删除评估报告吗", function() {
			//点击确认后的回调函数
			$.showLoading();
			__getdata_deletecp['id'] = _id;
			factory.request('_project_deletecp', _id);
		}, function() {
			//点击取消后的回调函数
		});
	});

	// 绑定删除项目按钮
	var submitDeleteProject = thin.submitDeleteProject;
	$(document).on('click', '[layout-mark=pro-del]', submitDeleteProject);
});

var __getdata_prokeyPush = {},
	__getdata_procph = {},
	__getdata_deletecp = {};

function __complete_procph(res, unkn) {
	$.hideLoading();
	if(res.status == 200) {
		chpdata[unkn] = res.data;
		createCph(res.data);
	} else {
		$.toptip(res.error, 'error');
	}
}

// 删除评估报告回调
function __complete_deletecp(res, unkn) {
	$.hideLoading();
	if (res.status == 204) {
		$.toast("删除成功", 960);
		setTimeout(function() {
			$('div[data-id=' + unkn + ']').remove();
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

var __getdata_checkcomplete = {};

// 检测是否完善项目
function isPerfectProject() {
	if($(this).hasClass('off')) return;
	var _id = $(this).attr('data-id');
	$.showLoading();
	__getdata_checkcomplete['project_id'] = _id;
	factory.request('_project_checkcomplete', $(this));
}

// 检测是否完善项目回调
function __complete_checkcomplete(res, unkn) {
	$.hideLoading();
	if (res.status == 200) {
		if (res.data.ret == true) {
			submitKeyPush(unkn);
		} else {
			var ele = '';
			ele += '<div style="line-height: 32px; text-align: left;">';
			ele += '以下信息尚未完善：';
			for(var i in res.data.msg) {
				ele += '<br />';
				ele += '<font class="m-r-1x">·</font>' + res.data.msg[i];
			}
			ele += '</div>';
			$.modal({
				title: "提示",
				text: ele,
				buttons: [{
						text: "取消",
						className: "default"
					},
					{
						text: "完善项目",
						onClick: function() {
							window.location.href = '/mobile/project/create?proid=' + unkn.attr('data-id') + '#perfect';
						}
					}
				]
			});
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

// 一鍵融資
function submitKeyPush(obj) {
	__getdata_prokeyPush['project_id'] = obj.attr('data-id');
	var ele = $('#aa-info').html();
	ele += '<p class="text-center m-t-2x m-x-1x fs12r">融资轮次：' + obj.attr('data-round') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs12r">所属领域：' + obj.attr('data-industry') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs12r">所在城市：' + obj.attr('data-city') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs12r">拟融资金额：' + obj.attr('data-money') + '万元</p>';
	$.modal({
		title: "",
		text: ele,
		buttons: [{
				text: "取消",
				className: "default"
			},
			{
				text: "一键投递",
				onClick: function() {
					$.showLoading();
					factory.request('_project_keyPush');
				}
			}
		]
	});
}

var chpdata = {};

function createCph(data) {
	var ele = '';
	for(var i in data) {
		ele += '<div class="weui-cell" data-id="' + data[i].id + '">';
		ele += '<div class="weui-cell__hd m-r-3x">';
		ele += '<i class="icon-files-pdf fs32r c-ff9e80"></i>';
		ele += '</div>';
		ele += '<div class="weui-cell__bd">';
		ele += '<p class="fs14r">' + tool.formatDate('yy-mm-dd hh:ii', data[i].create_time) + '</p>';
		ele += '</div>';
		ele += '<div class="weui-cell__ft">';
		ele += '<a href="javascript:;" class="m-l-2x" id="btn-delpdf" data-id="' + data[i].id + '">';
		ele += '<i class="icon-delete fs18r"></i>';
		ele += '</a>';
		ele += '<a href="' + data[i].cp_uri + '" class="m-l-2x">';
		ele += '<i class="icon-download fs18r"></i>';
		ele += '</a>';
		ele += '</div>';
		ele += '</div>';
	}
	$('#cph-modal-list').html(ele);
	checkLocation('cphlist');
}

function __complete_prokeyPush(res) {
	$.hideLoading();
	if(res.status == 201) {
		var str = '';
		str += '本次投递给了' + res.data.count + '位投资人<br />';
		str += '当前项目今日剩余' + res.data.surplus + '投递次数';
		$.alert(str, "提示", function() {});
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_procxlist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.length > 0) {
			showProjectList(res.data);
		} else {
			checkLocation('notlists');
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

function showProjectList(data) {
	$.showLoading();
	var ele = '';
	for(var i in data) {
		ele += '<div class="swiper-slide">';
		ele += '<div class="project-list position-relative">';
		ele += '<a href="javascript:;" layout-mark="pro-del" class="m-del fs14r" data-proid="' + data[i].id + '" data-iscp="' + data[i].is_cp_pay + '" data-iscx="' + data[i].is_cx_pay + '"> <i class="icon-delete v-middle"></i></a>';
		if(data[i].is_cx_pay == 1) {
			ele += '<img src="/public/home/images/cx_pay_on.png" class="cx_pay_st" />';
		} else {
			ele += '<img src="/public/home/images/cx_pay_off.png" class="cx_pay_st" />';
		}
		ele += '<div class="p-y-2x fund-bg c-fff" id="aa-info">';
		ele += '<img class="pic-circle m-x-auto bg-ffffff" src="' + data[i].logo + '" />';
		ele += '<p class="text-center m-t-2x fs18r">' + data[i].title + '</p>';
		ele += '<p class="text-center m-t-2x m-x-1x fs12r">' + data[i].words + '</p>';
		ele += '</div>';
		ele += '<div class="weui-grids m-t-2x">';
		ele += '<a class="weui-grid" href="/mobile/chain/project?proid=' + data[i].id + '#details" >';
		ele += '<div class="weui-grid__icon">';
		//ele += '<i class="icon-to_view fs24r"></i>';
		ele += '<img src="/public/home/images/icon-img/fund-1.png" />';
		ele += '</div>';
		ele += '<p class="weui-grid__label">预览项目</p>';
		ele += '</a>';
		ele += '<a class="weui-grid" href="/mobile/project/create?proid=' + data[i].id + '#basics" >';
		ele += '<div class="weui-grid__icon">';
		//ele += '<i class="icon-edit fs24r"></i>';
		ele += '<img src="/public/home/images/icon-img/fund-2.png" />';
		ele += '</div>';
		ele += '<p class="weui-grid__label">编辑项目</p>';
		ele += '</a>';
		ele += '<a class="weui-grid" href="/mobile/project/create?proid=' + data[i].id + '#perfect" >';
		ele += '<div class="weui-grid__icon">';
		//ele += '<i class="icon-configure fs24r"></i>';
		ele += '<img src="/public/home/images/icon-img/fund-3.png" />';
		ele += '</div>';
		ele += '<p class="weui-grid__label">完善项目</p>';
		ele += '</a>';
		ele += '<a class="weui-grid" href="/mobile/project/review?proid=' + data[i].id + '#industry" >';
		ele += '<div class="weui-grid__icon">';
		//ele += '<i class="icon-menu_aj fs24r"></i>';
		ele += '<img src="/public/home/images/icon-img/fund-4.png" />';
		ele += '</div>';
		ele += '<p class="weui-grid__label">项目评估</p>';
		ele += '</a>';
		ele += '<a class="weui-grid" href="javascript:;" data-id="' + data[i].id + '" id="btn-cph">';
		ele += '<div class="weui-grid__icon">';
		//ele += '<i class="icon-files-pdf fs24r"></i>';
		ele += '<img src="/public/home/images/icon-img/fund-5.png" />';
		ele += '</div>';
		ele += '<p class="weui-grid__label">评估报告</p>';
		ele += '</a>';
		if(data[i].is_cx_pay == 0) {
			ele += '<a class="weui-grid btn-jncxbzj" href="javascript:;" data-id="' + data[i].id + '">';
			ele += '<div class="weui-grid__icon">';
			//ele += '<i class="icon-financing fs24r"></i>';
			ele += '<img src="/public/home/images/icon-img/fund-6.png" />';
			ele += '</div>';
			ele += '<p class="weui-grid__label">缴纳保证金</p>';
			ele += '</a>';
		}
		ele += '</div>';
		ele += '<div class="position-absolute position-bottom position-screen">';
		/*ele += '<p class="fs12r p-l-2x c-fff">未缴纳诚信保证金，不能一键融资</p>';*/
		ele += '<div for="weuiAgree" class="weui-agree fs12r p-b-2x text-center">';
		ele += '<input checked="checked" id="weuiAgree" type="checkbox" class="weui-agree__checkbox m-r-1x">';
		ele += '<span class="weui-agree__text">';
		ele += '阅读并同意<a href="javascript:;" class="open-popup" data-target="#weuiAgree-modal">《价值链服务协议》</a>';
		ele += '</span>';
		ele += '</div>';
		ele += '<div class="b-line-t p-x-5x ftbox" >';
		if(data[i].is_cx_pay == 0) {
			ele += '<a href="javascript:;" class="keypush off">一键融资</a>';
		} else {
			ele += '<a href="javascript:;" class="keypush on" id="btn-keyPush" data-id="' + data[i].id + '" data-city="' + data[i].city + '" data-round="' + data[i].round + '" data-industry="' + data[i].industry + '" data-money="' + data[i].money + '">一键融资</a>';
		}
		ele += '</div>';
		ele += '</div>';
		ele += '</div>';
		ele += '</div>';
	}
	$('#project-lists').html(ele);
	thin.imports('/public/plug/JQueryWeui/swiper.min.js', function() {
		$.hideLoading();
		$(".swiper-container").swiper({
			speed: 400,
			spaceBetween: 100,
			initialSlide: tool.getSearch('index') || 0
		});
	});
}

var jude_list = false;

// 路由
function checkLocationCallback(hash) {
	if(hash == 'prolists') {
		if(!jude_list) {
			$.showLoading();
			factory.request('_project_cxlist');
			jude_list = true;
		}
	}

	if(hash == 'cphlist') {
		if(!__getdata_procph['project_id']) {
			checkLocation('prolists');
		}
	}
}